package com.wwwarehouse.usercenter.fragment.review;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.AuditListBean;
import com.wwwarehouse.usercenter.bean.response.InviteInfoResponseBean;
import com.wwwarehouse.usercenter.bean.response.ReviewInviteeResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static android.content.ContentValues.TAG;
import static com.wwwarehouse.usercenter.constant.UserCenterConstant.KEY_INVITE_CHECK_LIST;
import static com.wwwarehouse.usercenter.constant.UserCenterConstant.KEY_INVITE_CHECK_POSITION;


/**
 * @author : mundane
 * @time : 2017/8/16 13:34
 * @description :
 * @file : ReviewRefuseFragment.java
 */

public class ReviewRefuseFragment extends BaseFragment {

	private ArrayList<String> mProblemList;
	private LinearLayout mLl;
	private View mTvContactPerson;
	private EasyPopupWindow mEasyPopupWindow;
	private ReviewInviteeResponseBean mResponseBean;
	private TextView mTvInvitee;
	private Button mBtnReviewFinish;
	private Button mBtnReviewNext;
	private String mCardUkid;
	// 待审核的集合
	private List<InviteInfoResponseBean.InviteDetailsBean> mInviteeList;
	// 在集合中的位置
	private int mCurrentPosition;

	public static ReviewRefuseFragment newInstance(Bundle bundle) {
		ReviewRefuseFragment fragment = new ReviewRefuseFragment();
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			mResponseBean = bundle.getParcelable(ReviewInviteeFragment.BUNDLE_KEY);
//			mCardUkid = bundle.getString(ReviewInviteeFragment.KEY_CARD_UKID);
			Serializable serializable = bundle.getSerializable(KEY_INVITE_CHECK_LIST);
			if (serializable != null) {
				mInviteeList = (List<InviteInfoResponseBean.InviteDetailsBean>) serializable;
				mCurrentPosition = bundle.getInt(KEY_INVITE_CHECK_POSITION);
				mCardUkid = mInviteeList.get(mCurrentPosition).getCardUkid();
			}else{
				mCardUkid = bundle.getString(ReviewInviteeFragment.KEY_CARD_UKID);
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_refuse_review, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mLl = $(R.id.ll);
		mTvInvitee = $(R.id.tv_invitee);
		mBtnReviewFinish = $(R.id.btn_review_finish);
		mBtnReviewNext = $(R.id.btn_review_next);
		// 已经是最后一个了, 没有下一个了
		if (mInviteeList == null ||mCurrentPosition == mInviteeList.size() - 1) {
			mBtnReviewNext.setVisibility(View.GONE);
		}
		mBtnReviewNext.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showProgressDialog();
				Map<String, Object> hashMap = getStringObjectMap();
				NoHttpUtils.httpPost(UserCenterConstant.SAVE_AUDIT, hashMap, onResponseListener, 1);
			}
		});
		mBtnReviewFinish.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showProgressDialog();
				Map<String, Object> hashMap = getStringObjectMap();
				NoHttpUtils.httpPost(UserCenterConstant.SAVE_AUDIT, hashMap, onResponseListener, 0);
			}
		});
		mTvInvitee.setText(String.format("被邀请人: %s", mResponseBean.name.inviteeInfo));
		if (mResponseBean.faceUrl.checked) {
			addUnReason(mResponseBean.faceUrl.unReason);
		}
		if (mResponseBean.inviteDate.checked) {
			addUnReason(mResponseBean.inviteDate.unReason);
		}
		if (mResponseBean.mobile.checked) {
			addUnReason(mResponseBean.mobile.unReason);
		}
		if (mResponseBean.name.checked) {
			addUnReason(mResponseBean.name.unReason);
		}
		if (mResponseBean.personSex.checked) {
			addUnReason(mResponseBean.personSex.unReason);
		}
		if (mResponseBean.registerCity.checked) {
			addUnReason(mResponseBean.registerCity.unReason);
		}
		if (mResponseBean.registerDate.checked) {
			addUnReason(mResponseBean.registerDate.unReason);
		}
		// 联系被邀请人
		mTvContactPerson = $(R.id.tv_contact_person);
		mTvContactPerson.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				mEasyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.layout_contact_person, getContext(), mTvContactPerson, true, childClickListener);
			}
		});
	}

	@NonNull
	private Map<String, Object> getStringObjectMap() {
		Map<String, Object> hashMap = new HashMap<>();
		hashMap.put("cardUkid", mCardUkid);
		hashMap.put("auditResult", "1");
		List<AuditListBean> list = new ArrayList<>();
		if (mResponseBean.faceUrl.checked) {
			list.add(new AuditListBean(mResponseBean.faceUrl.type, mResponseBean.faceUrl.unReason));
		}
		if (mResponseBean.inviteDate.checked) {
			list.add(new AuditListBean(mResponseBean.inviteDate.type, mResponseBean.inviteDate.unReason));
		}
		if (mResponseBean.mobile.checked) {
			list.add(new AuditListBean(mResponseBean.mobile.type, mResponseBean.mobile.unReason));
		}
		if (mResponseBean.name.checked) {
			list.add(new AuditListBean(mResponseBean.name.type, mResponseBean.name.unReason));
		}
		if (mResponseBean.personSex.checked) {
			list.add(new AuditListBean(mResponseBean.personSex.type, mResponseBean.personSex.unReason));
		}
		if (mResponseBean.registerCity.checked) {
			list.add(new AuditListBean(mResponseBean.registerCity.type, mResponseBean.registerCity.unReason));
		}
		if (mResponseBean.registerDate.checked) {
			list.add(new AuditListBean(mResponseBean.registerDate.type, mResponseBean.registerDate.unReason));
		}
		if (mResponseBean.registerUrl.checked) {
			list.add(new AuditListBean(mResponseBean.registerUrl.type, mResponseBean.registerUrl.unReason));
		}

		hashMap.put("auditList", list);
		return hashMap;
	}

	private Bundle getBundle() {
		Bundle bundle = new Bundle();
		bundle.putSerializable(KEY_INVITE_CHECK_LIST, (Serializable) mInviteeList);
		bundle.putInt(KEY_INVITE_CHECK_POSITION, mCurrentPosition + 1);
		return bundle;
	}


	NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
		@Override
		public void onSucceed(CommonClass commonClass, int what) {
			switch (what) {
				case 0:
					if (TextUtils.equals("0", commonClass.getCode())) {
//						ToastUtils.showToast(commonClass.getMsg());
						EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
					} else {
						ToastUtils.showToast(commonClass.getMsg());
					}
					break;
				case 1:
					if (TextUtils.equals("0", commonClass.getCode())) {
//						ToastUtils.showToast(commonClass.getMsg());
						popFragment();
						ReviewInviteeFragment reviewInviteeFragment = new ReviewInviteeFragment();
						reviewInviteeFragment.setArguments(getBundle());
						pushFragment(reviewInviteeFragment);
					} else {
						ToastUtils.showToast(commonClass.getMsg());
					}
					break;
			}
		}

		@Override
		public void onFailed(String result, int what) {
			Log.d(TAG, result);
		}

		@Override
		public void onFinish(int what) {
			dismissProgressDialog();
		}
	};

	private void addUnReason(String unReason) {
		TextView textView = new TextView(getContext());
		textView.setText(unReason);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER_HORIZONTAL;
		params.topMargin = getResources().getDimensionPixelSize(R.dimen.common_dimen_dp10);
		textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.common_dimen_s7_dp14));
		mLl.addView(textView, params);
	}

	EasyPopupWindow.ChildClickListener childClickListener = new EasyPopupWindow.ChildClickListener() {
		@Override
		public void getChildView(int layoutId, View view) {
			view.findViewById(R.id.btn_wechat_contact).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					try {
						Intent intent = new Intent(Intent.ACTION_MAIN);
						ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
						intent.addCategory(Intent.CATEGORY_LAUNCHER);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.setComponent(cmp);

						startActivity(intent);
					} catch (Exception e) {
						Toast.makeText(getContext(), "检查到您手机没有安装微信，请安装后使用该功能", Toast.LENGTH_LONG).show();
					}
				}
			});

			view.findViewById(R.id.btn_phone_contact).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mResponseBean.mobile.inviteeInfo));
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

					startActivity(intent);
				}
			});

			view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mEasyPopupWindow.dismiss();
				}
			});
		}
	};

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ReviewRefuseFragment) {
			mActivity.setTitle("审核被邀请人");
		}
	}

	@Override
	public void requestDatas() {

	}
}
